import { call, put, takeEvery } from 'redux-saga/effects';
import * as actionTypes from '@src/model/actionType';
import * as API from '@src/api/index';
import { get as _get } from 'lodash';
import { notification } from 'tea-component';
import { openWindow, getQueryVariable } from '@src/utils/index';

function* fetchDecimal(action) {
  const { params = {} } = action;
  try {
    const data = yield call(API.getDecimal, params);
    yield put({
      type: actionTypes.homeDecimal,
      payload: {
        BlockHeight: _get(data, ['Data', 'BlockHeight'], 0),
        TransactionNum: _get(data, ['Data', 'TransactionNum'], 0),
        RunningTime: _get(data, ['Data', 'RunningTime'], 0),
        NodeNum: _get(data, ['Data', 'NodeNum'], 0),
        ChainNum: _get(data, ['Data', 'ChainNum'], 0),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchLatestContract() {
  try {
    const data = yield call(API.getLatestContract);
    yield put({
      type: actionTypes.homeLatestContract,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchLatestChain() {
  try {
    const data = yield call(API.getLatestChain);
    yield put({
      type: actionTypes.homeLatestChain,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchLatestBlockList(action) {
  const { params = {} } = action;
  try {
    const data = yield call(API.getLatestBlockList, params);
    yield put({
      type: actionTypes.latestBlockList,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchLatestTxList(action) {
  const { params = {} } = action;
  try {
    const data = yield call(API.getLatestTxList, params);
    yield put({
      type: actionTypes.latestTxList,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchNodeList(action) {
  try {
    const data = yield call(API.getNodeList, action.params);
    yield put({
      type: actionTypes.nodeList,
      payload: {
        TotalCount: _get(data, ['TotalCount'], 0),
        GroupList: _get(data, ['GroupList'], []),
      },
    });
  } catch (err) {
    console.error(err);
  }
}

function* fetchDetail(action) {
  const chainId = decodeURIComponent(getQueryVariable('chainid') || '');
  try {
    const data = yield call(API.getDetail, action.params);
    const type = _get(data, ['Data', 'Type'], 3);
    const id = _get(data, ['Data', 'Id'], 0);
    const blockHash = _get(data, ['Data', 'BlockHash'], '');
    if (type === 0) {
      openWindow(
        `/blockdetail?id=${encodeURIComponent(id)}&blockHash=${encodeURIComponent(
          blockHash,
        )}&chainid=${encodeURIComponent(chainId)}`,
      );
    } else if (type === 1 || type === 2) {
      openWindow(`/tsdetail?id=${encodeURIComponent(id)}&tp=ts&chainid=${encodeURIComponent(chainId)}`);
    } else {
      notification.error({
        title: '查询信息有误',
        description: '您查询的信息有误，请修改后再查询',
      });
    }
  } catch (err) {
    console.error(err);
  }
}

const sagas = [
  takeEvery(actionTypes.fetchDecimal, fetchDecimal),
  takeEvery(actionTypes.fetchLatestContract, fetchLatestContract),
  takeEvery(actionTypes.fetchLatestChain, fetchLatestChain),
  takeEvery(actionTypes.fetchLatestBlockList, fetchLatestBlockList),
  takeEvery(actionTypes.fetchLatestTxList, fetchLatestTxList),
  takeEvery(actionTypes.fetchNodeList, fetchNodeList),
  takeEvery(actionTypes.fetchDetail, fetchDetail),
];

export default sagas;
